#!/bin/sh
set -e

HOST_SYSTEM=$(uname -s)
CMAKE_ARGS="-DBUILD_TESTS:BOOL=True"

case "$CONFIG" in
full*)
    CMAKE_ARGS="-DBUILD_DOCUMENTATION:BOOL=True -DBUILD_EXAMPLES:BOOL=True -DWITH_OPUS:BOOL=True -DWITH_SPEEX:BOOL=True -DWITH_THEORA:BOOL=True -DWITH_VPX:BOOL=True"
    ;;
esac

case "$CONFIG" in
*debug*)
    CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_BUILD_TYPE:STRING=Debug -DBUILD_INTERNAL_TESTS:BOOL=True"
    ;;
esac

if [ $HOST_SYSTEM = "Darwin" ]; then
    # pick up cmake files from homebrew
    CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_PREFIX_PATH=/usr/local/opt/qt5"
else
    # This hides all the paths that contain custom travis binaries like the
    # updated version of cmake.
    export PATH=/usr/sbin:/usr/bin:/sbin:/bin
fi

# build with code coverage
if [ "$CONFIG" = "full-debug" ]; then
    export CXXFLAGS="-fprofile-arcs -ftest-coverage"
fi

# compile
mkdir build
cd build
cmake .. $CMAKE_ARGS

if [ $HOST_SYSTEM = "Darwin" ]; then
    make -k -j$(sysctl -n hw.logicalcpu)
else
    make -k -j$(nproc)
fi

# run tests
make test
